﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WoBian.DA.Interface;
using WoBian.DA;
using System.Data;
using WoBian.Entities.Admin;
using Oracle.DataAccess.Client;
using WoBian.Common;
using WoBian.Lib.Enum;
namespace WoBian.DA.Oracle.Admin.User
{
    public class AdminUserDAL : DataAccessBase, IAdminUserDAL, IDataAccessLayer
    {
        public IDbTransaction Transaction
        {
            get;
            set;
        }
        public IDbConnection Connection
        {
            get;
            set;
        }
        public AdminUserDAL(IDbConnection connection)
            : base(connection)
        {
            Connection = connection;
        }

        public AdminUserDAL(IDbTransaction transaction)
            : base(transaction)
        {
            Transaction = transaction;
        }

        public AdminUser GetById(string aId)
        {
            string aSql = string.Format(@"SELECT * FROM {0} WHERE {1} = :{2} AND ROWNUM = 1"
                , WoBian.Lib.ConstString.Tables.ADMIN_USER
                , AdminUser.USERID, AdminUser.USERID);
            List<IDataParameter> aList = new List<IDataParameter>();
            aList.Add(AddParameter(AdminUser.USERID, aId, DbType.String));
            DataRow dr = ExecuteDataRow(aSql, aList);
            AdminUser aAdminUser = dr.DataRowToModel<AdminUser>();
            return aAdminUser;
        }

        public string Save(AdminUser aAdminUser, DbOperationType aType)
        {
            string aSql = string.Empty;
            switch (aType)
            {
                case DbOperationType.Insert:
                    aAdminUser.UserId = Guid.NewGuid().ToString("D");
                    aSql = string.Format(
                @"INSERT INTO ADMIN_USER(USERID,NAME,PASSWORD,NICKNAME,TRUENAME,MOBILEPHONE,EMAILADDRESS,IDNUMBER,ADDRESS,ISLOCK,AGE,PICTURE,REMARK,
             CREATEID,CREATEDATE,UPDATEID,UPDATEDATE)
              VALUES(:USERID,:NAME,:PASSWORD,:NICKNAME,:TRUENAME,:MOBILEPHONE,:EMAILADDRESS,:IDNUMBER,:ADDRESS,:ISLOCK,:AGE,:PICTURE,:REMARK,
             :CREATEID,:CREATEDATE,:UPDATEID,:UPDATEDATE)");
                    break;
                case DbOperationType.Update:
                    aSql = string.Format("UPDATE {0} SET {1}={2} WHERE {3} = :{4}", WoBian.Lib.ConstString.Tables.ADMIN_USER, AdminUser.ADDRESS, AdminUser.ADDRESS, AdminUser.USERID, AdminUser.USERID);
                    break;
                default:
                    throw new Exception("数据库行为出现异常!");
            }

            List<IDataParameter> aList = new List<IDataParameter>();
            aList.Add(AddParameter("USERID", aAdminUser.UserId, DbType.String));
            aList.Add(AddParameter("NAME", aAdminUser.Name, DbType.String));
            aList.Add(AddParameter("PASSWORD", aAdminUser.Password, DbType.String));
            aList.Add(AddParameter("NICKNAME", aAdminUser.NickName, DbType.String));
            aList.Add(AddParameter("TRUENAME", aAdminUser.TrueName, DbType.String));
            aList.Add(AddParameter("MOBILEPHONE", aAdminUser.MobilePhone, DbType.String));
            aList.Add(AddParameter("EMAILADDRESS", aAdminUser.EMailAddress, DbType.String));
            aList.Add(AddParameter("IDNUMBER", aAdminUser.IdNumber, DbType.String));
            aList.Add(AddParameter("ADDRESS", aAdminUser.Address, DbType.String));
            aList.Add(AddParameter("ISLOCK", aAdminUser.IsLock, DbType.String));
            aList.Add(AddParameter("AGE", aAdminUser.Age, DbType.Int32));
            aList.Add(AddParameter("PICTURE", DBNull.Value, DbType.Binary));
            aList.Add(AddParameter("REMARK", aAdminUser.Remark, DbType.String));
            aList.Add(AddParameter("CREATEID", aAdminUser.CreateId, DbType.String));
            aList.Add(AddParameter("CREATEDATE", aAdminUser.CreateDate, DbType.Date));
            aList.Add(AddParameter("UPDATEID", aAdminUser.CreateId, DbType.String));
            aList.Add(AddParameter("UPDATEDATE", aAdminUser.UpdateDate, DbType.Date));
            ExecuteNonQuery(aSql, aList);
            return aAdminUser.UserId;
        }

        public string Delete(string aId)
        {
            throw new NotImplementedException();
        }


        public AdminUser GetByUserName(string UserName)
        {
            string aSql = string.Format(@"SELECT * FROM {0} WHERE {1} = :{2} AND ROWNUM = 1"
                 , WoBian.Lib.ConstString.Tables.ADMIN_USER
                 , AdminUser.NAME, AdminUser.NAME);
            List<IDataParameter> aList = new List<IDataParameter>();
            aList.Add(AddParameter(AdminUser.NAME, UserName , DbType.String));
            DataRow dr = ExecuteDataRow(aSql, aList);
            AdminUser aAdminUser = dr.DataRowToModel<AdminUser>();
            return aAdminUser;
        }

        [Obsolete]
        private AdminUser ToAdminUser(IDataReader dr)
        {
            AdminUser aAdminUser = null;
            if (dr.Read())
            {
                aAdminUser = new AdminUser();
                aAdminUser.UserId = dr[AdminUser.USERID].ToNullableString();
                aAdminUser.Name = dr[AdminUser.NAME].ToNullableString();
                aAdminUser.Address = dr[AdminUser.ADDRESS].ToNullableString();
                aAdminUser.Age = dr[AdminUser.AGE].ToInt32();
                aAdminUser.CreateDate = dr[AdminUser.CREATEDATE].ToDateTime();
                aAdminUser.CreateId = dr[AdminUser.CREATEID].ToNullableString();
                aAdminUser.EMailAddress = dr[AdminUser.EMAILADDRESS].ToNullableString();
                aAdminUser.IdNumber = dr[AdminUser.IDNUMBER].ToNullableString();
                aAdminUser.IsLock = dr[AdminUser.ISLOCK].ToNullableString();
                aAdminUser.MobilePhone = dr[AdminUser.MOBILEPHONE].ToNullableString();
                aAdminUser.NickName = dr[AdminUser.NICKNAME].ToNullableString();
                aAdminUser.Password = dr[AdminUser.PASSWORD].ToNullableString();
                aAdminUser.Picture = dr[AdminUser.PICTURE].ToBytes();
                aAdminUser.Remark = dr[AdminUser.REMARK].ToNullableString();
                aAdminUser.TrueName = dr[AdminUser.TRUENAME].ToNullableString();
                aAdminUser.UpdateDate = dr[AdminUser.UPDATEDATE].ToDateTime();
                aAdminUser.UpdateId = dr[AdminUser.UPDATEID].ToNullableString();
            }
            return aAdminUser;
        }
    }
}
